﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using WebApplication1.Models;

namespace WebApplication1.TagHelpers
{
    [RestrictChildren("Department", "Address")] //限定该标签的子元素必须是:Department和Address
    [HtmlTargetElement("Employee")]
    public class EmployeeTagHelper : TagHelper
    {
        [HtmlAttributeName("for-id")]
        public int Id { get; set; }

        [HtmlAttributeName("for-name")]
        public string Name { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var sb = new StringBuilder();
            sb.AppendFormat($"<p>{Id}</p>");
            sb.AppendFormat($"<p>{Name}</p>");

            sb.AppendFormat("<ul>");

            output.PreContent.SetHtmlContent(sb.ToString());

            output.PostContent.SetHtmlContent("</ul>");

        }
    }

    public class DepartmentTagHelper : TagHelper
    {
        //ModelExpression:模型表达式,可以映射属性、对象
        [HtmlAttributeName("for-departmentName")]
        public ModelExpression DepartmentName { get; set; }

        //[HtmlAttributeName("depmodel")]
        //public ModelExpression Department { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            //如果传过来的是对象,这样取即可
            //Department dep=Department.Model as Department;

            output.TagName = "a";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.SetAttribute("href", "http://www.baidu.com");
            output.Content.SetContent(DepartmentName.Model.ToString());

            //如果是字符串类型: ForName.Name就是属性名,ForName.Model.ToString():属性值
            //属性名
            var name = DepartmentName.Name;
            //属性值
            var value = DepartmentName.Model.ToString();


        }
    }
}
